# -*- coding: utf-8 -*-
#
#
##

##
#=== Rakuten genre search API wrapper
#   http://webservice.rakuten.co.jp/api/genresearch/
# 
class GenreSearch < RakutenApiBase
	#
	#=== search method 
	#
	def searchRootGenre(genreId)
		prm = {}
		prm['genreId'] = genreId
		
		prm['developerId'] = @developerId
		prm['affiliateId'] = @affiliateId

		iss = @@rakuten_settings['genresearch']
		prm['operation'] = iss['operation']
		prm['version'] = iss['version']

		prm['genrePath'] = iss['genrePath']

		xmltext = getWebContents(@restUrl,prm)
		unless xmltext or xmltext != ''
			return false
		end
		
		xobj = parseXMLObject(xmltext)
		unless xobj
			return false
		end

		genlist = REXML::XPath.first( xobj, 'Body/genreSearch:GenreSearch')
		if genlist and genlist.elements[1] and genlist.elements[1].elements['genreLevel'] then
			if genlist.elements[1].elements['genreLevel'].text.to_i == 1 then
				ret = {}
				ret[:genreId] = genlist.elements[1].elements['genreId'].text.to_i
				ret[:genreLevel] = genlist.elements[1].elements['genreLevel'].text.to_i
				ret[:genreName] = genlist.elements[1].elements['genreName'].text
				return ret
			end
		end
		return false
	end

	#
	def self.isSpecial(genreId)
		gl = GenreSearch.getGenreList()
		if gl.key?(genreId)
			return gl[genreId]['special']
		end
		return 0
	end
	#
	#=== test method
	#
	def self.test()
		gs = GenreSearch.new
		grt = gs.searchRootGenre("100316")
		pp grt
 		#pp is.search('4946842501915')
	end
	#
	#=== test method
	#
	def self.test2()
		gs = {
			'result'=>{'status'=>1,'message'=>'success'},
			'data'=>{
				'user'=>{'id'=>231,'created_at'=>'2011-10-17T15:00:00+09:00'},
				'potlist'=>[
					{'id'=>1,'item_num'=>5,'special_num'=>1,'img_url'=>'http://bakohead.info/img/pot/1_5_1.png','web_url'=>'http://bakohead.info/user/231/1'},
					{'id'=>2,'item_num'=>3,'special_num'=>0,'img_url'=>'http://bakohead.info/img/pot/2_3_0.png','web_url'=>'http://bakohead.info/user/231/2'},
					{'id'=>3,'item_num'=>1,'special_num'=>0,'img_url'=>'http://bakohead.info/img/pot/4_8_0.png','web_url'=>'http://bakohead.info/user/231/4'},
					{'id'=>4,'item_num'=>8,'special_num'=>0,'img_url'=>'http://bakohead.info/img/pot/4_8_0.png','web_url'=>'http://bakohead.info/user/231/4'},
					{'id'=>5,'item_num'=>10,'special_num'=>2,'img_url'=>'http://bakohead.info/img/pot/5_10_2.png','web_url'=>'http://bakohead.info/user/231/5'}
				]
			}
		}
		print gs.to_json
 		#pp is.search('4946842501915')
	end
	#
	def self.test3()
		gs = {
			'result'=>{'status'=>102,'message'=>'completed'},
			'data'=>{
				'user'=>{'id'=>231,'created_at'=>'2011-10-17T15:00:00+09:00'},
				'potlist'=>[
					{'id'=>1,'item_num'=>5,'special_num'=>1,'img_url'=>'http://bakohead.info/img/pot/1_5_1.png','web_url'=>'http://bakohead.info/user/231/1'},
					{'id'=>4,'item_num'=>8,'special_num'=>0,'img_url'=>'http://bakohead.info/img/pot/4_8_0.png','web_url'=>'http://bakohead.info/user/231/4'},
					{'id'=>5,'item_num'=>10,'special_num'=>2,'img_url'=>'http://bakohead.info/img/pot/5_10_2.png','web_url'=>'http://bakohead.info/user/231/5'}
				]
			}
		}
		print gs.to_json
 		#pp is.search('4946842501915')
	end
	def self.test4()
		gs = {
			'result'=>{'status'=>101,'message'=>'missing'},
			'data'=>{
				'user'=>{'id'=>231,'created_at'=>'2011-10-17T15:00:00+09:00'},
				'potlist'=>[]
			}
		}
		print gs.to_json
 		#pp is.search('4946842501915')
	end

	def self.test5()
		gs = {
			'result'=>{'status'=>1,'message'=>'success'},
			'data'=>{
				'user'=>{'id'=>231,'created_at'=>'2011-10-17T15:00:00+09:00'}
			}
		}
		print gs.to_json
 		#pp is.search('4946842501915')
	end

	def self.test6()
		gs = {
			'result'=>{'status'=>900,'message'=>'error'}
		}
		print gs.to_json
 		#pp is.search('4946842501915')
	end

	def self.test7()
		gs = {
			'result'=>{'status'=>1,'message'=>'success'},
			'data'=>{
				'user'=>{'id'=>231,'created_at'=>'2011-10-17T15:00:00+09:00'},
				'pot'=>{'id'=>1,'item_num'=>5,'special_num'=>1,'img_url'=>'http://bakohead.info/img/pot/1_5_1.png','web_url'=>'http://bakohead.info/user/231/1'},
				'itemlist'=>[
					{'id'=>1,'barcode'=>"4901777215672",'special'=>0,'created_at'=>'2011-10-17T15:00:00+09:00',
						'itemname'=>"サントリーBOSS ボスシルキーブラック リフレッシュ＆スムース　ボトル缶400g×24本入",
						'img_url'=>"http://thumbnail.image.rakuten.co.jp/@0_mall/mrk-09shop/cabinet/suntory/silkyblack.jpg?_ex=128x128",
						'web_url'=>"http://hb.afl.rakuten.co.jp/hgc/0e9c6142.8a3ab32d.0e9c6143.684cd463/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fmrk-09shop%2Fd4901777215672%2F&m=http%3A%2F%2Fm.rakuten.co.jp%2Fmrk-09shop%2Fi%2F10003142%2F"
					},
					{'id'=>2,'barcode'=>"4946842501915",'special'=>0,'created_at'=>'2011-10-17T15:00:10+09:00',
						'itemname'=>"アサヒフード　ミンティア　ドライハード10入",
						'img_url'=>"http://thumbnail.image.rakuten.co.jp/@0_mall/takaoka/cabinet/01781683/4946842501915.gif?_ex=128x128",
						'web_url'=>"http://hb.afl.rakuten.co.jp/hgc/0e9c6142.8a3ab32d.0e9c6143.684cd463/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Ftakaoka%2F4946842501915%2F&m=http%3A%2F%2Fm.rakuten.co.jp%2Ftakaoka%2Fi%2F10017357%2F"
					},
					{'id'=>3,'barcode'=>"4987035089315",'special'=>0,'created_at'=>'2011-10-17T15:00:20+09:00',
						'itemname'=>"【送料無料サービス】大塚製薬オロナミンC　120ml×50本【ドラッグピュア】",
						'img_url'=>"http://thumbnail.image.rakuten.co.jp/@0_mall/drugpure/cabinet/4987035089322.jpg?_ex=128x128",
						'web_url'=>"http://hb.afl.rakuten.co.jp/hgc/0e9c6142.8a3ab32d.0e9c6143.684cd463/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fdrugpure%2F4987035089315%2F&m=http%3A%2F%2Fm.rakuten.co.jp%2Fdrugpure%2Fi%2F10008192%2F"
					},
					{'id'=>4,'barcode'=>"4981254548608",'special'=>1,'created_at'=>'2011-10-17T15:00:30+09:00',
						'itemname'=>"《お取り寄せ》BUFFALO(バッファロー)耐衝撃/セキュリティー機能搭載 USB2.0用 ポータブルHDD 1TB ルビーレッド[HD-PXT1.0TU2-RD]【P10倍10/31まで】",
						'img_url'=>"http://thumbnail.image.rakuten.co.jp/@0_mall/akibakan/cabinet/img57928177.jpg?_ex=128x128",
						'web_url'=>"http://hb.afl.rakuten.co.jp/hgc/0e9c6142.8a3ab32d.0e9c6143.684cd463/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Fakibakan%2F10018414%2F&m=http%3A%2F%2Fm.rakuten.co.jp%2Fakibakan%2Fi%2F10018414%2F"
					},
					{'id'=>5,'barcode'=>"4901872507924",'special'=>0,'created_at'=>'2011-10-17T15:00:40+09:00',
						'itemname'=>"【ポイント2倍】お得な3個セット！資生堂UNO（ウーノ）　クールボディーミスト230ml×3（お取り寄せ品）　4901872507924*3【30dw09】【03dw10】【07dw10】",
						'img_url'=>"http://thumbnail.image.rakuten.co.jp/@0_mall/fukuei/cabinet/shohin105/500-7924-001a.jpg?_ex=128x128",
						'web_url'=>"http://hb.afl.rakuten.co.jp/hgc/0e9c6142.8a3ab32d.0e9c6143.684cd463/?pc=http%3A%2F%2Fitem.rakuten.co.jp%2Ffukuei%2F500-7924-003%2F&m=http%3A%2F%2Fm.rakuten.co.jp%2Ffukuei%2Fi%2F10058063%2F"
					}
				]
			}
		}

		print gs.to_json
 		#pp is.search('4946842501915')
	end


end


